package org.example.leetcode.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.leetcode.codeExecutor.api.CodeExecutorService;
import org.example.leetcode.common.domain.FileDto;
import org.example.leetcode.common.exception.MyException;
import org.example.leetcode.common.pojo.GlobalResponse;
import org.example.leetcode.common.utils.UserInfo;
import org.example.leetcode.core.domain.SysFile;
import org.example.leetcode.core.dao.SysFileMapper;
import org.example.leetcode.core.service.SysFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.leetcode.core.utils.CommonUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhc
 * @since 2024-04-29
 */
@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
    public static final String FILE="/FILE";
    public static final String BUCKET="/leetcode";
    @Resource
    CodeExecutorService codeExecutorService;
    @Override
    @Transactional
    public String upload(MultipartFile file, String type, String data) {
        String suffix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //删除先前的
        List<SysFile> sysFiles = this.baseMapper.selectList(new LambdaQueryWrapper<SysFile>()
                .eq(SysFile::getType, type)
                .eq(SysFile::getData, data));
        List<String> urls = sysFiles.stream().map(SysFile::getUrl).collect(Collectors.toList());
        this.baseMapper.deleteBatchIds(sysFiles.stream().map(SysFile::getId).collect(Collectors.toList()));
        GlobalResponse<Boolean> booleanGlobalResponse = codeExecutorService.removeFile(urls);
        if(!booleanGlobalResponse.getCode().equals(String.valueOf(HttpStatus.OK.value()))){
            throw new MyException("上传文件失败");
        }
        //添加新的
        String objectName = CommonUtils.calculateSHA256(file);
        String s1= String.valueOf(objectName.charAt(0));
        String s2= String.valueOf(objectName.charAt(1));
        GlobalResponse<String> stringGlobalResponse = codeExecutorService.uploadFileToMinio(file,FILE+"/" + s1 + "/" + s2 + "/" + objectName+suffix);
        if(!stringGlobalResponse.getCode().equals(String.valueOf(HttpStatus.OK.value()))){
            throw new MyException("上传文件失败");
        }
        //保存记录到表中
        SysFile sysFile = new SysFile();
        sysFile.setData(data);
        sysFile.setUrl(FILE+"/" + s1 + "/" + s2 + "/" + objectName+suffix);
        sysFile.setType("1");
        this.save(sysFile);
        return FILE+"/" + s1 + "/" + s2 + "/" + objectName+suffix;
    }
}
